package com.yang.spring.core;

import com.yang.spring.core.bean.builder.post.BeanPostHandler;
import com.yang.spring.core.bean.definition.BeanDefinition;
import com.yang.spring.core.bean.BeanFactory;
import com.yang.spring.core.bean.definition.parser.BeanDefinitionParser;
import java.util.List;
import java.util.Map;

public class SpringApplication {
    public static void run(Class clazz, String[] args) {
        // 解析beanDefinition
        List<BeanDefinition> beanDefinitionList = parserBeanDefinitionList(clazz);

        // 初始化bean
        initBeans(beanDefinitionList);

        // 后置处理bean
        postHandleBeans(beanDefinitionList);
    }

    private static void postHandleBeans(List<BeanDefinition> beanDefinitionList) {
        BeanPostHandler beanPostHandler = new BeanPostHandler();
        beanPostHandler.postHandleBeans(beanDefinitionList);
    }

    private static List<BeanDefinition> parserBeanDefinitionList(Class clazz) {
        BeanDefinitionParser beanDefinitionParser = new BeanDefinitionParser();
        return beanDefinitionParser.parseBeanDefinitionList(clazz);
    }

    private static void initBeans(List<BeanDefinition> beanDefinitionList) {
        BeanFactory.initBeans(beanDefinitionList);
//        for (Map.Entry<String, Object> entry : BeanFactory.getClassName2BeanMap().entrySet()) {
//            String beanName = entry.getKey();
//            Object instance = entry.getValue();
//            System.out.println(beanName + ":" + instance);
//        }
    }
}
